package com.liujit.bms.admin.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liujit.bms.admin.domain.model.BlogArticleComment;
import com.liujit.bms.admin.domain.params.BlogArticleCommentParams;
import com.liujit.bms.admin.domain.query.BlogArticleCommentQuery;
import com.liujit.bms.admin.domain.result.BlogArticleCommentResult;
import com.liujit.bms.admin.mapper.BlogArticleCommentMapper;
import com.liujit.bms.admin.service.BlogArticleCommentService;
import com.liujit.common.domain.params.BaseParams;
import com.liujit.common.domain.result.PageBean;
import com.liujit.common.exception.DataNotFoundException;
import com.liujit.common.utils.DozerConvertor;
import com.liujit.common.utils.Pagination;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Description 博客文章评论Service实现类
 * @Author liujun
 * @Date 2021-07-19 11:16:42
 */
@Service
public class BlogArticleCommentServiceImpl extends ServiceImpl<BlogArticleCommentMapper, BlogArticleComment>
        implements BlogArticleCommentService {

    @Override
    public PageBean<BlogArticleCommentResult> getPage(BlogArticleCommentQuery query) {
        Page<BlogArticleComment> page = Pagination.page(query);
        query.setPid(0L);
        IPage<BlogArticleCommentResult> iPage = super.baseMapper.selPage(page, query);
        return PageBean.page(iPage);
    }

    @Override
    public Boolean reply(BlogArticleCommentParams params) {
        BlogArticleComment blogArticleComment = super.getById(params.getId());
        if (blogArticleComment == null) {
            throw new DataNotFoundException();
        }
        BlogArticleComment entity = new BlogArticleComment();
        entity.setArticleId(blogArticleComment.getArticleId());
        entity.setPid(params.getId());
        entity.setMemberId(1L);
        entity.setComment(params.getComment());
        entity.setIsOpen(Convert.toShort(1));
        entity.setCommentTime(new Date());
        super.save(entity);
        return super.updateById(new BlogArticleComment() {{
            setId(params.getId());
            setIsOpen(Convert.toShort(1));
        }});
    }

    @Override
    public BlogArticleCommentResult get(Long id) {
        BlogArticleComment blogArticleComment = super.getById(id);
        if (blogArticleComment == null) {
            throw new DataNotFoundException();
        }
        return DozerConvertor.convertor(blogArticleComment, BlogArticleCommentResult.class);
    }

    @Override
    public BlogArticleCommentResult getDetail(Long id) {
        BlogArticleCommentResult result = super.baseMapper.selById(id);
        if (result == null) {
            throw new DataNotFoundException();
        }
        return result;
    }

    @Override
    public Boolean del(BaseParams params) {
        BlogArticleComment blogArticleComment = super.getById(params.getId());
        if (blogArticleComment == null) {
            throw new DataNotFoundException();
        }
        LambdaQueryWrapper<BlogArticleComment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BlogArticleComment::getPid, params.getId());
        super.remove(wrapper);
        return super.removeById(params.getId());
    }
}
